using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using GrapeCity.Forguncy.Log;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace LiteVectorIndex
{
    [Icon("pack://application:,,,/LiteVectorIndex;component/Resources/Icon.png")]
    [Category("嵌入式向量库")]
    [OrderWeight(90)]
    public class Cluster_Info : Command, ICommandExecutableInServerSideAsync, IServerCommandParamGenerator
    {
        [FormulaProperty]
        [DisplayName("集合名")]
        public required object KBName { get; set; }

        [DisplayName("将集合的信息保存到变量")]
        [Description("包含有Dimension、MetricType、NormalizeOnAddAndQuery、Count四个属性")]
        [ResultToProperty]
        public string ResultInParamater { get; set; } = "CollectionInfo";


        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var name = (await dataContext.EvaluateFormulaAsync(KBName))?.ToString() ?? "";

            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("集合名不能为空，请检查输入。");
            }

            if (CollectionManager.Instance.CollectionExists(name))
            {
                var collection = CollectionManager.Instance.GetCollection(name);
                var info = new CollectionInfo()
                {
                    Dimension = collection.Dimension,
                    MetricType = collection.MetricTypeInUse.ToString(),
                    NormalizeOnAddAndQuery = collection.NormalizeOnAddAndQuery,
                    Count = collection.Count
                };
                dataContext.Log.AppendLine($"向量集合：{name} 信息获取成功，共 {info.Count} 条数据，维度：{info.Dimension}，类型：{info.MetricType}，同一化：{info.NormalizeOnAddAndQuery}。");
                dataContext.Parameters[ResultInParamater] = info;
                return new ExecuteResult();
            }
            else
            {
                dataContext.Log.AppendLine($"向量集合：{name} 不存在。");
                // 返回null表示集合不存在
                dataContext.Parameters[ResultInParamater] = null;

            }

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "嵌入式向量库：获取集合信息";
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }

        public IEnumerable<GenerateParam> GetGenerateParams()
        {
            yield return new GenerateObjectParam()
            {
                ParamName = this.ResultInParamater,
                Description = "集合的信息",
                ParamScope = CommandScope.All,
                SubPropertiesDescription = new Dictionary<string, string>() {
                    { "Dimension","向量的维度数量"},
                    { "MetricType","向量距离的算法"},
                    { "NormalizeOnAddAndQuery","自动执行同一化"},
                    { "Count","集合中向量的数量"}
                }
            };
        }

        public class CollectionInfo
        {
            public int Dimension { get; set; }
            public string MetricType { get; set; }
            public bool NormalizeOnAddAndQuery { get; set; }
            public long Count { get; set; }
        }
    }
}
